<template>
  <!-- 项目工地管理 -->
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="工地名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入工地名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>



      <el-form-item label="所属省" prop="provinceCode">
        <el-select v-model="queryParams.provinceCode" clearable  placeholder="请选择所属省" @change="provinceChange">
          <el-option
            v-for="(v,index) in provinceList"
            :key="index"
            :label="v.provinceName"
            :value="v.provinceCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属市" prop="cityCode">
        <el-select v-model="queryParams.cityCode" clearable  placeholder="请选择所属市" @change="cityChange">
          <el-option
            v-for="(v,index) in cityList"
            :key="index"
            :label="v.cityName"
            :value="v.cityCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属县区" prop="areaCode">
        <el-select v-model="queryParams.areaCode" clearable  placeholder="请选择区域" >
          <el-option
            v-for="(v,index) in areaList"
            :key="index"
            :label="v.areaName"
            :value="v.areaCode"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="申报人名字" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入申报人名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="建设单位" prop="constructionUnit">
        <el-input
          v-model="queryParams.constructionUnit"
          placeholder="请输入建设单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <!-- <el-form-item label="运输企业" prop="transportCompany">
        <el-input
          v-model="queryParams.transportCompany"
          placeholder="请输入运输企业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="有效期开始日期" prop="startDate">
        <el-date-picker clearable
          v-model="queryParams.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择有效期开始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="有效期结束日期" prop="endDate">
        <el-date-picker clearable
          v-model="queryParams.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择有效期结束日期">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['construction:site:add']" >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['construction:site:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['construction:site:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['construction:site:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border fit highlight-current-row empty-text="暂无数据" height="600" v-loading="loading" :data="siteList" @selection-change="handleSelectionChange">
      <el-table-column fixed="left" type="selection" width="55" align="center" />
      <el-table-column fixed="left" label="工地名称" align="center" prop="name" />
      <el-table-column label="监控视频" align="center" prop="name" >
        <template slot-scope="scope">
        <el-button
            size="mini"
            type="text"
            @click="selectJianKong(scope.row)"
          >查看监控</el-button>
         </template>
      </el-table-column>

      <el-table-column label="所属省" align="center" prop="provinceName" />
      <el-table-column label="所属市" align="center" prop="cityName" />
      <el-table-column label="所属县区" align="center" prop="areaName" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="有效期开始日期" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期结束日期" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="建设单位" align="center" prop="constructionUnit" />
      <el-table-column label="施工单位" align="center" prop="constructionCompany" />
      <el-table-column label="申报人名字" align="center" prop="userName" />
      <el-table-column align="center" prop="isReportConstructionWaste" label="是否报备建筑垃圾处理方案">
        <template slot-scope="scope">
          <span v-if="scope.row.isReportConstructionWaste == 0">是</span>
          <span v-if="scope.row.isReportConstructionWaste == 1">否</span>
        </template>
      </el-table-column>
      
      <!-- <el-table-column label="建筑垃圾产生总量(吨)" align="center" prop="capacity" />
      <el-table-column label="经度" align="center" prop="latitude" />
      <el-table-column label="纬度" align="center" prop="longitude" />
      <el-table-column label="工地负责人" align="center" prop="leader" />
      <el-table-column label="所属企业名称" align="center" prop="enterpriseName" />
      <el-table-column prop="share" label="是否公开">
        <template slot-scope="scope">
          <span v-if="scope.row.share == 0">是</span>
          <span v-if="scope.row.share == 1">否</span>
        </template>
      </el-table-column>

      <el-table-column prop="isReportConstructionWaste" label="是否报备建筑垃圾处理方案">
        <template slot-scope="scope">
          <span v-if="scope.row.isReportConstructionWaste == 0">是</span>
          <span v-if="scope.row.isReportConstructionWaste == 1">否</span>
        </template>
      </el-table-column>

      <el-table-column prop="isVideoDevice" label="是否有监控设备">
        <template slot-scope="scope">
          <span v-if="scope.row.isVideoDevice == 0">是</span>
          <span v-if="scope.row.isVideoDevice == 1">否</span>
        </template>
      </el-table-column>

      <el-table-column label="审核状态" align="center" prop="auditFlowStatus" />
      <el-table-column label="回填方量" align="center" prop="callbackVolume" />
      <el-table-column label="申报人名字" align="center" prop="userName" /> -->


      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="generateQRCode(scope.row)">二维码</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['construction:site:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['construction:site:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看二维码 -->
    <el-dialog title="查看二维码" :visible.sync="qeCodeOpen" width="500px" append-to-body>
      <img :src="codeUrl" class="login-code-img center-image" style="width: 400px; min-height: 350px;" />
      <div slot="footer" class="dialog-footer">
        <a :href="codeUrl" download="qrcode.png">
          <el-button icon="el-icon-download" plain>下载二维码</el-button>
        </a>
        <el-button @click="cancelqeCode">取消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改项目工地信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" min-width="1000px" append-to-body>
      <dialogGDXX ref="refGDXX" :form="form" @submitForm="submitForm" :provinceList="provinceList"  />
    </el-dialog>





  </div>



</template>

<script>
import { listSite, getSite, delSite, addSite, updateSite,pageSite } from "@/api/construction/site";
import {provinceList,cityList,areaList} from '@/api/region/region'
import { generateQRCode } from "@/api/util/qrcode";

import dialogGDXX from './components/index'

export default {
  name: "Site",
  components: { dialogGDXX },
  data() {
    return {
      codeUrl: null,
      qeCodeOpen: false,
      provinceList:[],
      cityList:[],
      areaList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目工地信息表格数据
      siteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        provinceCode: null,
        provinceName: null,
        cityCode: null,
        cityName: null,
        areaCode: null,
        areaName: null,
        townName: null,
        townCode: null,
        name: null,
        address: null,
        startDate: null,
        endDate: null,
        capacity: null,
        regionId: null,
        latitude: null,
        longitude: null,
        leader: null,
        number: null,
        abbreviation: null,
        leaderPhone: null,
        type: null,
        organization: null,
        transportCompany: null,
        cargoType: null,
        constructionUnit: null,
        constructionCompany: null,
        designUnit: null,
        designUnitContact: null,
        designUnitPhone: null,
        supervisionUnit: null,
        supervisionUnitContact: null,
        measurementUnit: null,
        internalTransfer: null,
        externalTransfer: null,
        projectCategory: null,
        engineeringSand: null,
        constructionWaste: null,
        mud: null,
        srl: null,
        contractPrice: null,
        constructionScale: null,
        certificateNumber: null,
        star: null,
        landUseCertificate: null,
        constructionPlanPermit: null,
        constructionWasteDischargePermit: null,
        constructionContract: null,
        constructionPermit: null,
        teamId: null,
        enterpriseName: null,
        share: null,
        isReportConstructionWaste: null,
        isVideoDevice: null,
        exts: null,
        auditFlowStatus: null,
        key: null,
        callbackVolume: null,
        construcitonUserId: null,
        userName: null,
        surveyInformationUserId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.initProvinceList();
    this.initCityList(this.queryParams.provinceCode);

  },
  methods: {


    //显示二维码
    generateQRCode(row) {
      // 发送请求获取二维码图片
      generateQRCode(row.id)
        .then(response => {

          this.codeUrl = "data:image/gif;base64," + response.img;
          this.qeCodeOpen = true;
        })
        .catch(error => {
          console.error('Error:', error);
          // 处理错误情况
        });
    },


    initConstructionSiteList(){
     listSite().then(response => {
       this.constructionSiteList = response.data;
     })
   },
   initRegulationList(){
     listRegulation({type:0,status:0}).then(response => {
       this.regulationList = response.data;
     })
   },
   initProvinceList(){
       provinceList().then(response => {
         this.provinceList = response.data;
       })
   },
   initCityList(v){
       let obj={
         provinceCode:v
       }
       cityList(obj).then(response => {
         this.cityList = response.data;
       })
   },
   initAreaList(v){
       let obj={
         cityCode:v
       }
       areaList(obj).then(response => {
         this.areaList = response.data;
       })
   },
   //省份选择的
   provinceChange(v){
       this.initCityList(v);
       this.queryParams.cityCode=null;
       this.queryParams.areaCode = null;
   },
   cityChange(v){
     this.initAreaList(v);
     this.queryParams.areaCode = null;
   },
    /** 查询项目工地信息列表 */
    getList() {
      this.loading = true;
      pageSite(this.queryParams).then(response => {
        this.siteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelqeCode() {
      this.qeCodeOpen = false;
      this.codeUrl = null;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        provinceCode: null,
        provinceName: null,
        cityCode: null,
        cityName: null,
        areaCode: null,
        areaName: null,
        townName: null,
        townCode: null,
        name: null,
        address: null,
        startDate: null,
        endDate: null,
        capacity: null,
        regionId: null,
        latitude: null,
        longitude: null,
        leader: null,
        number: null,
        abbreviation: null,
        leaderPhone: null,
        type: null,
        organization: null,
        transportCompany: null,
        cargoType: null,
        constructionUnit: null,
        constructionCompany: null,
        designUnit: null,
        designUnitContact: null,
        designUnitPhone: null,
        supervisionUnit: null,
        supervisionUnitContact: null,
        measurementUnit: null,
        internalTransfer: null,
        externalTransfer: null,
        projectCategory: null,
        engineeringSand: null,
        constructionWaste: null,
        mud: null,
        srl: null,
        contractPrice: null,
        constructionScale: null,
        certificateNumber: null,
        star: null,
        remark: null,
        landUseCertificate: null,
        constructionPlanPermit: null,
        constructionWasteDischargePermit: null,
        constructionContract: null,
        constructionPermit: null,
        teamId: null,
        enterpriseName: null,
        share: null,
        isReportConstructionWaste: null,
        isVideoDevice: null,
        exts: null,
        auditFlowStatus: null,
        key: null,
        callbackVolume: null,
        construcitonUserId: null,
        userName: null,
        surveyInformationUserId: null,
        status: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },



    // 查看监控
    selectJianKong(id){

    // 新窗口的URL
      const url = 'http://4g.sjzhlkj.cn:9999/shipin.html';
      // 新窗口的特征，例如宽度和高度
      const windowFeatures = 'width=600,height=400';

      // 打开新窗口
      window.open(url, '_blank');

    },
    //省份选择的
    provinceChange(v){
      this.initCityList(v);
      this.queryParams.cityCode=null;
      this.queryParams.areaCode = null;
    },
    cityChange(v){
      this.initAreaList(v);
      this.queryParams.areaCode = null;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目工地信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSite(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目工地信息";
        console.log(this.$refs['refGDXX'], "refGDXX")
        if(this.$refs['refGDXX']){
          if(this.form.cityCode){
              this.$refs['refGDXX'].initCityList(this.form.provinceCode)
          }
          if(this.form.areaCode){
              this.$refs['refGDXX'].initAreaList(this.form.cityCode)
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      
      this.open = false;
      this.getList();
      return
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目工地信息编号为"' + ids + '"的数据项？').then(function() {
        return delSite(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('construction/site/export', {
        ...this.queryParams
      }, `site_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

